import { defineConfig,loadEnv } from 'vite'
import react from '@vitejs/plugin-react'
import path from 'path'
import viteCompression from 'vite-plugin-compression';

// https://vitejs.dev/config/
export default ({mode})=>{
  let outDir:string = 'dist';
  const APPNAME:string = loadEnv(mode,process.cwd()).VITE_APP;
  const pro = loadEnv(mode,process.cwd());
  if(APPNAME === 'test'){
    outDir='test'
  }else if (APPNAME === 'production') { 
    outDir = 'pro';
  }else {
    outDir = 'dist';
  }

  return defineConfig({
    plugins: [
      react(),
      // viteCompression({
      //   threshold: 1024, 
      // })
    
    ],
    resolve:{
      alias:{
        '@':path.resolve(__dirname,('./src'))
      }
    },
    build:{
      outDir
    },
    define: {
      'process.env': pro
    },
    css: {
      preprocessorOptions: {
        less: {
          modifyVars: {
            "primary-color": "#EAA516", //全局样式
          }, // 可以在这里设置全局less变量
          javascriptEnabled: true, // 允许在less文件中使用JavaScript表达式
        },
      },
    },
    server:{
      proxy:{
        '/api':{
          target:'http://localhost:3000',
          changeOrigin:true,
          rewrite:path => path.replace(/^\/api/, ''),
        }
      }
    },
    optimizeDeps: {
      include: [
        'axios',
        'react-redux',
        'react-router-dom',
        'redux',
      ],
    },
  })
}

